package tt.dz.order.util;

import tt.dz.common.DzDigester;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class UUIDUtil {
    private static int digitNum = 4;

    public static long longUUID() {
        return Math.abs(UUID.randomUUID().getMostSignificantBits());
    }

    public static String shortUUID() {
        long uuid = longUUID();
        char[] uuidStr = String.valueOf(uuid).toCharArray();
        StringBuilder shortUUIDStr = new StringBuilder(digitNum);
        int i = 0;
        for (i = 0; i < digitNum; i++) {
            shortUUIDStr.append(uuidStr[i]);
        }
        return shortUUIDStr.toString();
    }

    /**
     * 获取14位当前时间
     *
     * @return 14位当前时间
     */
    public static String getCurrentDate14(Date now) {
        if (now == null) now = new Date();
        return new SimpleDateFormat("yyyyMMddHHmmss").format(now);
    }

    /**
     * 获取当前时间
     */
    public static String getCurrentDate(Date now, String dataFormat) {
        if (now == null) now = new Date();
        if (dataFormat == null || dataFormat.length() == 0) dataFormat = "yyyyMMddHHmmss";
        return new SimpleDateFormat(dataFormat).format(now);
    }

    /**
     * 生成n位随机数
     *
     * @param n 位数
     * @return
     */
    public static String randomNum(int n) {
        StringBuilder builder = new StringBuilder(n);
        for (int i = 0; i < n; i++) {
            Random rnd = new Random(longUUID());
            builder.append((char) ('0' + rnd.nextInt(10)));
        }
        return builder.toString();
    }

    public static long getId(String tableNo) {
        String now = getCurrentDate14(null);
        StringBuilder sbuilder = new StringBuilder(tableNo);
        sbuilder.append(now).append(shortUUID());
        return Long.valueOf(sbuilder.toString());
    }

    public static void main(String[] args) {
//		System.out.println( DzDigester.decryptCommonInfo("88pKOU7tsqPSHwWdeuy7Zg=="));
        Map<String, String> params = new HashMap<String, String>();
        params.put("body", "支付宝支付");
        params.put("subject", "设备在线支付");
        params.put("sign_type", "RSA2");
        params.put("buyer_logon_id", "180****1232");
        params.put("auth_app_id", "2019050964412473");
        params.put("notify_type", "trade_status_sync");
        params.put("out_trade_no", "0104201906061610111470");
        params.put("point_amount", "0.00");
        params.put("version", "1.0");
        params.put("fund_bill_list", "[{\"amount\":\"0.01\"),\"fundChannel\":\"PCREDIT\"}]");
        params.put("buyer_id", "2088802501940818");
        params.put("total_amount", "0.01");
        params.put("trade_no", "2019060922001440810522027750");
        params.put("notify_time", "2019-06-09 09:32:14");
        params.put("charset", "utf-8");
        params.put("invoice_amount", "0.01");
        params.put("trade_status", "TRADE_SUCCESS");
        params.put("gmt_payment", "2019-06-09 09:32:13");
        params.put("sign", "JrtuyiSZUKXCOi1aPOZ05S+TUGpkvSwRSDP58F23zHRoMLj5ycO0zv3BmWkg6IMzYv/Sas07DRv6eYAc0Uo3VB0/MYdtEWDS5YX3kaTP4pHZK3+nBnu9y7CwdypU43v0wA76m2cuJbmTndI/kPmkg15ISSgYkkjbfdWjdI97t7e2qHtY4aiIA2WklQ+u7o2BYntcr18xokw5TKrrNx+YSfHztS4E+GvDrkbpZTcBHRNVkaajWTXZIX/HtHmoVioSuFFPDGBJqtvARCBQS4Ee6qgrNpvE7UP0xrpTOt/cOBImWIKZTQA8zP76FVeInYQ4zbuglhtYce0gdyziBvrmmg==");
        params.put("gmt_create", "2019-06-09 09:32:12");
        params.put("buyer_pay_amount", "0.01");
        params.put("receipt_amount", "0.01");
        params.put("app_id", "2019050964412473");
        params.put("seller_id", "2088431693222846");
        params.put("notify_id", "2019060900222093213040810578200626");
        params.put("seller_email", "scjtxny@163.com");
        System.out.println( AliPayUtil.verify(params));


//		Calendar c = Calendar.getInstance();
//		System.out.println(c.get(Calendar.YEAR));
//		System.out.println(c.get(Calendar.MONTH));
//		System.out.println(c.get(Calendar.DATE));
//		System.out.println(c.get(Calendar.HOUR_OF_DAY));
//		System.out.println(c.get(Calendar.MINUTE));
//		System.out.println(c.get(Calendar.SECOND));
//		DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
//		System.out.println(df.format(new Date()));
//		Set<String> set = new HashSet<String>();
//		for(int j = 0; j < 1000; j++){
//			String rNum = randomNum(6);
//			set.add(rNum);
//			System.out.println(rNum);
//		}
//		System.out.println(set.size());
//		/*
//		int i = 0;
//		for(i = 0; i < 10000; i++){
//			set.add(shortUUID());
//		}
//		System.out.println(set.size());
//		*/
    }

}
